//
//  CycleView.m
//  CycleShuffling
//
//  Created by 好米有易 on 2017/9/5.
//  Copyright © 2017年 好米有易. All rights reserved.
//

#import "CycleView.h"
#import "CycleItem.h"
@interface CycleView ()<UICollectionViewDataSource, UICollectionViewDelegate>

/** collectionView */
@property (nonatomic, strong) UICollectionView *colletionView;

/** flowLayout */
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

/** timer*/
@property (nonatomic, strong) NSTimer *timer;

/** pageControl*/
@property (nonatomic, strong) UIPageControl *pageControl;

@end

static NSString *CYCLE_ITEMID = @"CYCLE_ITEMID";

@implementation CycleView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        
        
    }
    
    return self;
}


- (void)setCycleData:(NSArray *)cycleData {
    _cycleData = cycleData;
    
    [self addSubview:self.colletionView];
    [self.colletionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    [self addSubview:self.pageControl];
    
    [self addTimer];
}

#pragma mark - 定时器 -
- (void)addTimer{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer ;
    
}

- (void)removeTimer{
    [self.timer invalidate];
    self.timer = nil;
}


- (void)nextPage {
    NSIndexPath *currentIndexPath = [[self.colletionView indexPathsForVisibleItems] lastObject];
    
    NSInteger nextItem = currentIndexPath.item + 1;
    
    if (nextItem == self.cycleData.count - 1) {
        
        nextItem = 1;
        
        
        [self.colletionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        
        return;
    }
    
    NSLog(@"nextItem - %@", @(nextItem));
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:0];
    
    [self.colletionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}


#pragma mark - lazy UI -

- (UICollectionView *)colletionView {
    
    if (_colletionView == nil) {
        _colletionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
        [_colletionView registerClass:[CycleItem class] forCellWithReuseIdentifier:CYCLE_ITEMID];
        _colletionView.delegate = self;
        _colletionView.dataSource = self;
        _colletionView.bounces = NO;
        _colletionView.pagingEnabled = YES;
        _colletionView.backgroundColor = [UIColor brownColor];
        _colletionView.showsHorizontalScrollIndicator = NO;
    }
    return _colletionView;
}


- (UICollectionViewFlowLayout *)flowLayout {
    if (_flowLayout == nil) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = self.bounds.size;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.minimumLineSpacing = 0;
    }
    return _flowLayout;
}

- (UIPageControl *)pageControl {
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 50, self.bounds.size.width, 50)];
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor greenColor];
        _pageControl.numberOfPages = self.cycleData.count - 2;
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}


#pragma mark - UICollectionViewDataSource -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.cycleData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    CycleItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:CYCLE_ITEMID forIndexPath:indexPath];
    
    item.imgView.image = [UIImage imageNamed:self.cycleData[indexPath.row]];
    
    return item;
}


#pragma mark - UICollectionViewDelegate -
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
 
    self.indexBlock(indexPath.row);
    
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    [self addTimer];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    int index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    if (index == 0) {
    
        [self.colletionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.cycleData.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        
        return;
    }
    
    if (index == self.cycleData.count - 1) {
        
         [self.colletionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        
        return;
    }
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    int pageNumber = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
    
    NSLog(@"pageNumber %@", @(pageNumber));
    
    
    _pageControl.currentPage = pageNumber % self.cycleData.count - 1;

    

}





@end
